package com.kharthick.game;

import java.applet.Applet;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.File;

import com.kharthick.pwGame.map.Environment;
import com.kharthick.pwGame.map.Map;
import com.kharthick.pwGame.map.MapParser;
import com.kharthick.pwGame.map.Tile;
import com.kharthick.pwGame.map.TutorialMap;
import com.kharthick.pwGame.players.Player;
import com.kharthick.pwGame.rules.*;
import com.kharthick.pwGame.units.*;

public class Core extends Applet implements Runnable, MouseListener, MouseMotionListener {
	
	public static final int TILE_SIZE = 50;
	private static final long serialVersionUID = 4300370483474176231L;
	
	List<Map> maps;
	List<Player> players;
	Player currentPlayer;
	Environment currentEnv;
	Map currentMap;
	Player p1, p2;
	Unit s, a;
	Unit ActiveUnit;
	MediaTracker tracker;
	Thread core;
	Rules move, attack;
	int mouseX, mouseY;
	
	
    // The object we will use to write with instead of the standard screen graphics
    Graphics bufferGraphics;
    // The image that will contain everything that has been drawn on bufferGraphics.
    Image offscreen;
    Dimension dim;
	
	public void start() {
		currentEnv = new Environment("something", 2, tracker);
		currentMap = currentEnv.getMap();
		//currentMap = currentEnv.getMap();
		players = currentEnv.getPlayers();
		if(null == currentMap){
			System.out.println("Map not found");
		}
		
		move = new MoveRules();
		attack = new AttackRules();
		
		core = new Thread(this);
		core.start();
	}
	
	public void run() {
		System.out.println("Hi");
		//int count = 0;
		while(core != null){
			currentPlayer = players.get(0);
			try{
				core.sleep(100);
				//System.out.print(count);
				//count++;
			} catch (InterruptedException e) {}
			if(null != ActiveUnit){
				//System.out.println("ActiveUnit not null");
				//System.out.println(move.actionOk(ActiveUnit, mouseX, mouseY, players));
				//System.out.println(attack.actionOk(ActiveUnit, mouseX, mouseY, players));
				
				if(move.actionOk(ActiveUnit, mouseX, mouseY, players)){
					System.out.println("move");
					move.doAction(currentMap);
					ActiveUnit = null;
					repaint();
				}
				else if(attack.actionOk(ActiveUnit, mouseX, mouseY, players)){
					System.out.println("attack");
					attack.doAction(currentMap);
					ActiveUnit = null;
					repaint();
				}
			}
			repaint();
		}
		System.out.println("Bye");
	}
	
	public void init() {
		setSize(500, 400);
		
		//Initiate tracker to wait for image load
		tracker = new MediaTracker(this);
		
		//add mouse listeners
		addMouseListener(this);
		addMouseMotionListener(this);
		
        // Create an offscreen buffer
		dim = getSize();
        offscreen = createImage(dim.width,dim.height);
        bufferGraphics = offscreen.getGraphics();
        repaint();
	}
	
	public void paint(Graphics g){
   	 	bufferGraphics.clearRect(0,0,dim.width,dim.width);
   	 	currentMap.draw(bufferGraphics);
   	 	//System.out.println("drawmap");
   	 	
   	 	for(Iterator it = players.iterator(); it.hasNext(); ) {
			Player p = (Player) it.next();
			p.drawDeadUnits(bufferGraphics);
		}
   	 	for(Iterator it = players.iterator(); it.hasNext(); ) {
			Player p = (Player) it.next();
			p.drawUnits(bufferGraphics);
		}
   	 	
		g.drawImage(offscreen,0,0,this);
	}
	
    public void update(Graphics g)
    {
         paint(g);
    }

	public void mouseClicked(MouseEvent me) {
		Tile t = currentMap.getTileByScreenPosition(me.getX(), me.getY());
		//System.out.println("currentPlayer="+currentPlayer);
		Unit u = currentPlayer.getUnitByScreenPosition(me.getX(), me.getY());
		
		
		/*Unit u = null;
		for(Iterator it = players.iterator(); it.hasNext(); ) {
			boolean oneSelected = false;
			Player p = (Player) it.next();
			if(null != p.getUnitByScreenPosition(me.getX(), me.getY())){
				u = p.getUnitByScreenPosition(me.getX(), me.getY());
				oneSelected = true;
			}
			if(oneSelected = false)
				u = null;				
		}*/
		
		mouseX = me.getX();
		mouseY = me.getY();
		if(null != t) {
			switch (me.getButton()) {
				case MouseEvent.BUTTON1:
					t.leftClick();
					break;
				case MouseEvent.BUTTON2:
				case MouseEvent.BUTTON3:
					t.rightClick();
					break;
			}
			repaint();
		}
		if(null != u){
			switch (me.getButton()) {
				case MouseEvent.BUTTON1:
					u.leftClick();
					ActiveUnit = u;
					break;
			}
		}
	}

	public void mousePressed(MouseEvent me) {
		// TODO Auto-generated method stub
		
	}

	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void mouseDragged(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void mouseMoved(MouseEvent me) {
		Tile t = currentMap.getTileByScreenPosition(me.getX(), me.getY());
		if(null != t) {
			t.mouseOver();
			//repaint();
		}
	} 
	
}
